﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager
{
    public class DictionariesRepository : BaseRepository, IDictionariesRepository
    {
        public DictionariesRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {
        }
        /// <summary>
        /// 根据字典类别key获取下面的字典选项
        /// </summary>
        /// <param name="typeKey">字典类别key</param>
        /// <returns></returns>
        public List<DictionariesPo> GetDictionariesKey(string typeKey)
        {
            return _systemCenterDbSoaContext.Dictionariess.Where(c => c.dic_type_key == typeKey).ToList();
        }
        /// <summary>
        /// 分页条件查询
        /// </summary>
        /// <param name="dicName"></param>
        /// <param name="dicType"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<DictionariesPo> GetDictionariesList(string dicName,string dicType,int pageIndex,int pageSize/*SearchModel searchModel*/)
        {
            //获取全部数据
            var query = _systemCenterDbSoaContext.Dictionariess.AsQueryable();
            if(!string.IsNullOrWhiteSpace(dicName))
            {
                query = query.Where(t => t.dic_name.Contains(dicName));
            }
            if(!string.IsNullOrWhiteSpace(dicType))
            {
                query = query.Where(t => t.dic_type_key == dicType);
            }
            return query.ToList();
            #region
            ////判断有没有搜索条件
            //if(searchModel.SearchItem!=null&&searchModel.SearchItem.Count>0)
            //{
            //    //把搜索条件拿出来
            //    Dictionary<string, string> temDi = searchModel.SearchItem;
            //    //搜索字段dic_name
            //    string dicName = "dic_name";
            //    //判断搜索条件中是否有dic_name
            //    if(temDi.Keys.Contains(dicName)&&!string.IsNullOrWhiteSpace(temDi[dicName]))
            //    {
            //        query = query.Where(c => c.dic_name.Contains(temDi[dicName]));
            //    }
            //    //搜索字段为dic_type_key
            //    string dicType = "dic_type_key";
            //    //判断搜索条件中是否有dic_type_key
            //    if(temDi.Keys.Contains(dicType)&& !string.IsNullOrWhiteSpace(temDi[dicType]))
            //    {
            //        query = query.Where(c => c.dic_type_key == temDi[dicType]);
            //    }
            //}
            #endregion
         }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dictionaries"></param>
        /// <returns></returns>
        public bool GetDictionariesAdd(DictionariesPo dictionaries)
        {
            DictionariesPo diction = ModelCopier.CreateInstanceFrom<DictionariesPo>(dictionaries);
            diction.creator_name = "wf";
            diction.creator_date = DateTime.Now;
            _systemCenterDbSoaContext.Dictionariess.Add(diction);

            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool GetDictionariesDel(string ids)
        {
            var ArrId = ids.Split(',');
            foreach(var item in ArrId)
            {
                int id = Convert.ToInt32(item);
                _systemCenterDbSoaContext.Dictionariess.Remove(_systemCenterDbSoaContext.Dictionariess.Find(id));
            }
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DictionariesPo GetDictionariesFan(int id)
        {
            return _systemCenterDbSoaContext.Dictionariess.Find(id);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dictionaries"></param>
        /// <returns></returns>
        public bool GetDictionariesUpdate(DictionariesPo dictionaries)
        {
            _systemCenterDbSoaContext.Entry(dictionaries).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
    }
}
